package com.pdstudio.weather;

import java.util.ArrayList;
import java.util.List;

import com.pdstudio.app.Application;
import com.pdstudio.app.Application.EventHandler;
import com.pdstudio.bean.ArticleDetail;
import com.pdstudio.bean.Warning;
import com.pdstudio.http.LiveHttpMethod;
import com.pdstudio.http.WeatherHttpMethod;
import com.pdstudio.util.SharePreferenceUtil;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class WarningActivity extends Activity implements EventHandler {

	private View customLiveIndexTitleView;
	private TextView txtTitle;
	private ImageView ivBack;
	private TextView tvWarningCity;
	private TextView tvWarningLevel;
	private TextView tvWarningTime;
	private TextView tvWarningGuide;
	private ImageView ivWarning;
	private Handler handler = null;
	private SharePreferenceUtil mSpUtil;
	private Application mApplication;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_warning);
		
		Application.mListeners.add(this);
		mApplication = Application.getInstance();
		mSpUtil = mApplication.getSharePreferenceUtil();

		LinearLayout layout = (LinearLayout) findViewById(R.id.activity_warning);
		int resId = mApplication.getApplicationBackground(mSpUtil
				.getWeatherStatus());
		layout.setBackground(getResources().getDrawable(resId));


		customLiveIndexTitleView = findViewById(R.id.title_warning);
		txtTitle = (TextView) (customLiveIndexTitleView
				.findViewById(R.id.title2_text_nav));
		txtTitle.setText("预警信号");

		ivBack = (ImageView) customLiveIndexTitleView
				.findViewById(R.id.title2_back);
		ivBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				WarningActivity.this.finish();
			}

		});

		tvWarningCity = (TextView) findViewById(R.id.warning_city);
		tvWarningLevel = (TextView) findViewById(R.id.warning_level);
		tvWarningTime = (TextView) findViewById(R.id.warning_time);
		tvWarningGuide = (TextView) findViewById(R.id.warning_guide);
		ivWarning = (ImageView)findViewById(R.id.warning_img);
		// 设置textview可以滚动的代码
		tvWarningGuide.setMovementMethod(ScrollingMovementMethod.getInstance());

		// 开一条子线程加载网络数据
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(0);
					WeatherHttpMethod http = new WeatherHttpMethod(
							WarningActivity.this);
					List<Warning> detail = new ArrayList<Warning>();
					detail = http.GetWarning(mSpUtil.getAuthCode(),mApplication.getCurSimpleCity().cityCode);
					Message msg = handler.obtainMessage(0, detail);
					handler.sendMessage(msg);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		};

		try {
			// 开启线程
			new Thread(runnable).start();
			// handler与线程之间的通信及数据处理
			handler = new Handler() {
				public void handleMessage(Message msg) {
					if (msg.what == 0) {

						// msg.obj是获取handler发送信息传来的数据
						@SuppressWarnings("unchecked")
						List<Warning> detail = new ArrayList<Warning>();
						// Toast.makeText(getApplicationContext(), "数据加载中", 0)
						// .show();
						if (msg != null && msg.obj != null) {
							detail = (List<Warning>) msg.obj;
						}
						else{
							Warning w = new Warning();
							w.warningAddress="无";
							w.postTime="无";
							w.warningType="无";
							w.tip="当前没有预警信息";
							detail.add(w);
						}
						setWarning(detail.get(0));
					}
				}
			};
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void setWarning(Warning detail) {
		if (detail != null) {
			tvWarningCity.setText("预警城市：" + detail.warningAddress);
			tvWarningLevel.setText("预警类型：" + detail.warningType);
			tvWarningTime.setText("发布时间：" + detail.postTime);
			tvWarningGuide.setText(detail.tip);
			ivWarning.setImageResource(mApplication
					.getWarningIcon(detail.warningType));
		} else {
			// tvEmergencyTitle.setText("目前没有应急响应信息");
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.warning, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onCityComplite() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onNetChange() {
		// TODO Auto-generated method stub
		
	}
}
